package com.android.sunflower.ui.mygarden

import android.util.Log
import android.view.*
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import com.android.sunflower.R
import com.android.sunflower.databinding.MygardentListItemBinding
import com.android.sunflower.databinding.PlantListItemBinding
import com.android.sunflower.logic.room.entities.Flower
import com.android.sunflower.logic.room.entities.PlantedF
import com.android.sunflower.ui.MainActivity
import com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
import java.text.SimpleDateFormat
import java.util.*

class MyGardenListAdapter(val plantedFList:List<PlantedF>,val plantedFInfoList:List<Flower>):RecyclerView.Adapter<MyGardenListAdapter.ViewHolder>(){

    private var inDeleteMode=false

    private val deleteSet=HashSet<PlantedF>()

    private var actionMode:ActionMode?=null
    inner class ViewHolder(val binding:MygardentListItemBinding):RecyclerView.ViewHolder(binding.root)

    inner class MyActionModeCallback:ActionMode.Callback{
        override fun onCreateActionMode(mode: ActionMode?, menu: Menu?): Boolean {
            mode?.menuInflater?.inflate(R.menu.contextual_aoctionbar_menu,menu)
            return true
        }

        override fun onPrepareActionMode(mode: ActionMode?, menu: Menu?): Boolean {
            return true
        }

        override fun onActionItemClicked(mode: ActionMode?, item: MenuItem?): Boolean {
            when(item?.itemId){
                R.id.water->{
                    for(pl in deleteSet){
                        pl.waterTime=Date()
                    }
                    MyGardenFragment.viewModel?.updateItems(deleteSet.toList())
                    deleteSet.clear()
                    mode?.finish()
                    MyGardenFragment.myGardenBinding.selectAllButton.hide()
                    MyGardenFragment.myGardenBinding.selectNoneButton.hide()
                    inDeleteMode=false
                }
                R.id.delete->{
                        MyGardenFragment.viewModel?.deleteItems(deleteSet.toList())
                    deleteSet.clear()
                    mode?.finish()
                    MyGardenFragment.myGardenBinding.selectAllButton.hide()
                    MyGardenFragment.myGardenBinding.selectNoneButton.hide()
                    inDeleteMode=false
                }
            }
            return true
        }


        override fun onDestroyActionMode(mode: ActionMode?) {
            deleteSet.clear()
            inDeleteMode=false
            MyGardenFragment.myGardenBinding.selectAllButton.hide()
            MyGardenFragment.myGardenBinding.selectNoneButton.hide()
            //刷新
            notifyItemRangeChanged(0,plantedFList.size)
            MyGardenFragment.myGardenBinding.myGardenToolbar.visibility=View.VISIBLE
        }
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding=MygardentListItemBinding.inflate(LayoutInflater.from(MainActivity.runningInstance),parent,false)
        val viewHolder=ViewHolder(binding)
        //隐藏全选和全不选按钮
        if(!inDeleteMode){
            MyGardenFragment.myGardenBinding.selectAllButton.hide()
            MyGardenFragment.myGardenBinding.selectNoneButton.hide()
        }
        //为全选按钮和全不选按钮绑定点击监听
        MyGardenFragment.myGardenBinding.selectAllButton.setOnClickListener {
            for(pl in plantedFList){
                deleteSet.add(pl)
            }
            actionMode?.title="已选择${deleteSet.size}株植物"
            notifyItemRangeChanged(0,plantedFList.size)
        }

        MyGardenFragment.myGardenBinding.selectNoneButton.setOnClickListener {
            deleteSet.clear()
            actionMode?.title="已选择${deleteSet.size}株植物"
            notifyItemRangeChanged(0,plantedFList.size)
        }

        return viewHolder
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val name=plantedFList[position].plantName
        val f=plantedFInfoList[position]
        val df=SimpleDateFormat("yyyy-MM-dd")
        val plantTime=df.format(plantedFList[position].addTime)
        val waterTime=df.format(plantedFList[position].waterTime)
        val daysToWater= f?.waterPeriod?.minus((Date().date- plantedFList[position].waterTime?.date!!))
        holder.binding.baseInfo.text="${name}:${plantedFList[position].id} 种植于 ${plantTime}"
        holder.binding.waterInfo.text="上次浇水：${waterTime}.${daysToWater}天后浇水"
        holder.binding.plantedFPic.setImageResource(f?.picture!!)

        //判断当前是不是多选模式
        if(inDeleteMode){
            holder.binding.deleteSelectCB.visibility=View.VISIBLE
        }else holder.binding.deleteSelectCB.visibility=View.GONE

        //判断当前item是否已经被选中
        holder.binding.deleteSelectCB.isChecked = deleteSet.contains(plantedFList[position])

        //设置长按监听器
        holder.itemView.setOnLongClickListener {
            if(!inDeleteMode){
                inDeleteMode=true
                holder.binding.deleteSelectCB.isChecked=true
                deleteSet.add(plantedFList[position])
                //更新item的UI让CheckBox显示出来
                notifyItemRangeChanged(0,plantedFList.size)
                MyGardenFragment.myGardenBinding.myGardenToolbar.visibility=View.GONE
                actionMode=MainActivity.runningInstance?.startActionMode(MyActionModeCallback())
                actionMode?.title="已选择${deleteSet.size}株植物"

                MyGardenFragment.myGardenBinding.selectAllButton.show()
                MyGardenFragment.myGardenBinding.selectNoneButton.show()
            }
            true
        }

        //设置点击监听
        holder.itemView.setOnClickListener {
            if(inDeleteMode){
                if(holder.binding.deleteSelectCB.isChecked){
                    deleteSet.remove(plantedFList[position])
                }else{
                    deleteSet.add(plantedFList[position])
                }
                holder.binding.deleteSelectCB.isChecked = !holder.binding.deleteSelectCB.isChecked
                actionMode?.title="已选择${deleteSet.size}株植物"
            }
        }



    }

    override fun getItemCount(): Int {
        return plantedFList.size
    }
}